//
//  FinanceLogViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/7/10.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "FinanceLogViewController.h"
#import "FinanceLogTableViewCell.h"
#import "FinanceLog.h"
#import <MJRefresh.h>
#import "AppDelegate.h"
#import "KJSRequest.h"
#import "TransverseSelectView.h"
@interface FinanceLogViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,assign) int start;
@property (nonatomic,assign) int length;
@property (nonatomic,strong) NSMutableArray <FinanceLog*> *logs;

@property (weak, nonatomic) IBOutlet TransverseSelectView *transverseView;

@end

@implementation FinanceLogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"资金流水"];
    self.recordTableV.dataSource = self;
    self.recordTableV.delegate = self;
    self.cellType = investLog;
    
    self.transverseView.titleArray = @[@"投资流水",@"融资流水",@"充值提现"];
    self.transverseView.selectAction = ^(NSInteger selectLocation){
        [self configeration];
        NSLog(@"选择 %ld",(long)selectLocation);
        switch (selectLocation) {
            case 0:
                self.cellType = investLog;
                [self GetInvestLog];
                break;
            case 1:
                self.cellType = raiseLog;
                [self GetRaiseLog];
                break;
            case 2:
                self.cellType = cashLog;
                [self GetCashLog];
                break;
            default:
            break;
        }
    };

    [self configeration];
    [self configurateTableView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self GetInvestLog];
}
#pragma mark - configeration
- (void)configeration
{
    self.length = 3;
    self.start = 0;
    self.logs = [[NSMutableArray alloc] init];
    self.cellNum = 0;
}
- (void)configurateTableView {
    self.recordTableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    [footer setTitle:@"上拉加载更多数据" forState:MJRefreshStateIdle];
    [footer setTitle:@"用力拉！~" forState:MJRefreshStatePulling];
    [footer setTitle:@"更新数据中~" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据" forState:MJRefreshStateNoMoreData];
    
    self.recordTableV.mj_footer = footer;
    
    [self.recordTableV registerNib:[UINib nibWithNibName:@"FinanceLogView" bundle:nil] forCellReuseIdentifier:@"FinanceLogTableViewCell"];
}
#pragma mark - custom method
- (void)GetInvestLog
{
    if (CurentDelegate.account) {
        [KJSRequest GetUserInvestLogListWithPartyId:CurentDelegate.account.partyId start:self.start length:self.length ret:^(FinanceLogModel *model) {
            [self.logs addObjectsFromArray:model.aaData];
            [(MJRefreshAutoNormalFooter *)self.recordTableV.mj_footer setTitle:([model.aaData count] < ROW_OF_EACH_PAGE)? @"没有更多数据了" :@"上拉加载更多数据" forState:MJRefreshStateIdle];
            self.cellNum = [self.logs count];
            [self endRefresh];
            [self.recordTableV reloadData];
        }];
    }
}
- (void)GetRaiseLog
{
    if (CurentDelegate.account) {
        [KJSRequest GetUserRaiseLogListWithPartyId:CurentDelegate.account.partyId start:self.start length:self.length ret:^(FinanceLogModel *model) {
            [self.logs addObjectsFromArray:model.aaData];
            //去除融资成功的那条记录
            for (int i = 0 ; i < [self.logs count] ; i++) {
                if ([self.logs[i].orderPayModeName isEqualToString:@""]) {
                    [self.logs removeObjectAtIndex:i];
                }
            }
            [(MJRefreshAutoNormalFooter *)self.recordTableV.mj_footer setTitle:([model.aaData count] < ROW_OF_EACH_PAGE)? @"没有更多数据了" :@"上拉加载更多数据" forState:MJRefreshStateIdle];
            self.cellNum = [self.logs count];
            [self endRefresh];
            [self.recordTableV reloadData];
        }];
    }
}
- (void)GetCashLog
{
    if (CurentDelegate.account) {
        [KJSRequest GetUserCashLogListWithPartyId:CurentDelegate.account.partyId start:self.start length:self.length ret:^(FinanceLogModel *model) {
            [self.logs addObjectsFromArray:model.aaData];
            [(MJRefreshAutoNormalFooter *)self.recordTableV.mj_footer setTitle:([model.aaData count] < ROW_OF_EACH_PAGE)? @"没有更多数据了" :@"上拉加载更多数据" forState:MJRefreshStateIdle];
            self.cellNum = [self.logs count];
            [self endRefresh];
            [self.recordTableV reloadData];
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellNum;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FinanceLogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FinanceLogTableViewCell"];
    if([self.logs count] > indexPath.row){
        cell.log = self.logs[indexPath.row];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}
#pragma mark - refresh method
- (void)headerRefresh {
    [self configeration];
    switch (self.cellType) {
        case investLog:
            [self GetInvestLog];
            break;
        case raiseLog:
            [self GetRaiseLog];
            break;
        case cashLog:
            [self GetCashLog];
            break;
        default:
            break;
    }
}

- (void)footerRefresh {
    switch (self.cellType) {
        case investLog:
            self.start += self.length;
            [self GetInvestLog];
            break;
        case raiseLog:
            self.start += self.length;
            [self GetRaiseLog];
            break;
        case cashLog:
            self.start += self.length;
            [self GetCashLog];
            break;
        default:
            break;
    }
}

- (void)endRefresh {
    [self.recordTableV.mj_header endRefreshing];
    [self.recordTableV.mj_footer endRefreshing];
}

@end
